package model.xpdl2
{
	import mx.utils.UIDUtil;
	
	/*
	 * 
	 */
	public class Package{
		private var id:String;
		
		private var xpdl2:String;
		
		private var xsd:String;
		
		private var name:String;
		
		private var packageHeader:PackageHeader;
		
		private var processes:WorkflowProcesses;
		
		public function Package(){
			id=UIDUtil.createUID();
			name="test_name";
			xpdl2="http://www.wfmc.org/2004/XPDL2.0alpha";
			xsd="http://www.w3.org/2001/XMLSchema";
			packageHeader=new PackageHeader();
			processes=new WorkflowProcesses();
		}
		
		public function genXML():String{
			var xml:String="<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";
			var nsprefix:String="xpdl2";
			xml+="<"+nsprefix+":Package"+" xmlns:xpdl2=\""+xpdl2+"\" xmlns:xsd=\""+xsd+"\" Id=\""+id+"\" Name=\""+name+"\" >\n";
			xml+=packageHeader.genXML();
			xml+=processes.genXML();
			xml+="</Package>\n";	
			return xml;		
		}
		
		public function getProcesses():WorkflowProcesses{
			return this.processes;
		}
		
		public function initPackage(xml:XML):void{
			
			var ns:Namespace=xml.namespace("xpdl2");
			
			/**
			 * Package id and name
			 *  
			 */
			this.id=xml.attribute("Id").toString();
			this.name=xml.attribute("Name").toString();	
			
			/**
			 * Package Header
			 *  
			 */
			packageHeader=new PackageHeader();
				
			var packageHeaderXML:XML=new XML(xml.ns::PackageHeader);
			var versionXML:String=packageHeaderXML.ns::XPDLVersion;
			var createdXML:String=packageHeaderXML.ns::Created;
			var creatorXML:String=packageHeaderXML.ns::Creator;
			var vendorXML:String=packageHeaderXML.ns::Vendor;
			var desXML:String=packageHeaderXML.ns::Description;
			
			packageHeader.getVendor().setVendor(vendorXML);
			packageHeader.getCreated().setCreated(createdXML);
			packageHeader.getCreator().setCreator(creatorXML);
			packageHeader.getXpdlVersion().setVersion(versionXML);
			packageHeader.getDescription().setDescription(desXML);
			
			trace("Package Header----------------");
			trace(packageHeader.getDescription().getDescription());
			trace(packageHeader.getCreated().getCreated());
			trace("Package Header----------------");
			
			/**
			 * Process id and name
			 * 
			 */ 
			processes=new WorkflowProcesses();	
			
			var processXML:XML=new XML(xml.ns::WorkflowProcesses.ns::WorkflowProcess);
			processes.getProcess().id=processXML.attribute("Id").toString();
			processes.getProcess().name=processXML.attribute("Name").toString();
			
			trace(" Process id and name----------------");
			trace(processes.getProcess().id);
			trace(" Process id and name----------------");
			/**
			 * Process Header
			 * 
			 */ 	
			 var processHeaderXML:XML=new XML(xml.ns::WorkflowProcesses.ns::WorkflowProcess.ns::ProcessHeader);		 
			 processes.getProcess().getProcessHeader().getCreated().setCreated(processHeaderXML.ns::Created);
			 processes.getProcess().getProcessHeader().getDescription().setDescription(processHeaderXML.ns::Description);
			 processes.getProcess().getProcessHeader().getPersistent().persis=processHeaderXML.ns::Persistent;
			 
			 trace("Process Header----------------");
			 trace(processes.getProcess().getProcessHeader().getCreated().getCreated());
			 trace(processes.getProcess().getProcessHeader().getPersistent().persis);
			 trace("Process Header----------------");
			 
			 /**
			 * Activities
			 * 
			 */ 
			  var activitisXML:XML=new XML(xml.ns::WorkflowProcesses.ns::WorkflowProcess.ns::Activities);	
			 var i:int;
			  for(i=0;i<activitisXML.children().length();i++){
			 	
			 	var activityXML:XML=new XML(activitisXML.ns::Activity[i]);
			 	
			 	var activity:Activity=new Activity();
			 	
			 	activity.id=activityXML.attribute("Id").toString();			 	
			 	activity.name=activityXML.attribute("Name").toString();
			 	
			 	trace("Activity Id and Name-----------------");
			 	trace(activity.id);
			 	trace(activity.name);
			 	trace("Activity Id and Name-----------------");
			 			 
			 	if(activityXML.ns::Event.toString().length>0 && activityXML.ns::Event.toString().charAt(0)=='<'){
			 		
			 		var eventXML:XML=new XML(activityXML.ns::Event);
			 		
			 		var ev:Event=new Event();
			 					 		
			 		var eventType:String=new XML(eventXML.children()).name().toString();
			 		if(eventType==ns+"::StartEvent"){
			 			var startev:StartEvent=new StartEvent();
			 			ev.setStartEvent(startev);
			 			trace("Activity StartEvent-----------------");
			 			trace(ev.getStartEvent());
			 			trace("Activity StartEvent-----------------");
			 		}
			 		
			 		if(eventType==ns+"::EndEvent"){
			 			var endev:EndEvent=new EndEvent();
			 			ev.setEndEvent(endev);
			 			trace("Activity EndEvent-----------------");
			 			trace(ev.getEndEvent());
			 			trace("Activity EndEvent-----------------");
			 		}
			 		
			 		if(eventType==ns+"::IntermediateEvent"){
			 			var ineXML:XML=new XML(eventXML.ns::IntermediateEvent);
			 			var interev:IntermediateEvent=new IntermediateEvent();
			 			interev.target=ineXML.attribute("Target").toString();
			 			interev.trigger=ineXML.attribute("Trigger").toString();	
			 			ev.setInterEvent(interev);
			 			trace("Activity IntermediateEvent-----------------");
			 			trace(ev.getInterEvent().target);
			 			trace(ev.getInterEvent().trigger);
			 			trace("Activity IntermediateEvent-----------------");
			 		}
			 		
			 		activity.setEvent(ev);
			 		
			 	}
			 	
			 	if(activityXML.ns::Implementation.toString().length>0 && activityXML.ns::Implementation.toString().charAt(0)=='<'){
			 		
			 		var impleXML:XML=new XML(activityXML.ns::Implementation);
			 		var taskXML:XML=new XML(impleXML.ns::Task);
			 		var im:Implementation=new Implementation();
			 		
			 		var taskType:String=new XML(taskXML.children()).name().toString();
			 		if(taskType==ns+"::TaskScript"){
			 			var taskScript:TaskScript=new TaskScript();
			 			im.getTask().setTaskScript(taskScript);
			 		}
			 		if(taskType==ns+"::TaskUser"){
			 			var taskUser:TaskUser=new TaskUser();
			 			im.getTask().setTaskUser(taskUser);
			 		}
			 		if(taskType==ns+"::TaskService"){
			 			var taskService:TaskService=new TaskService();
			 			im.getTask().setTaskService(taskService);
			 		}
			 		
			 		activity.setImplementation(im);
			 		
			 		trace("Activity Task-----------------");
			 		trace(taskType);
			 		trace("Activity Task-----------------");
			 		
			 	}
			 	
			 	trace(activityXML.children()[0].name());
			 	if(activityXML.children()[0].name().toString()==ns+"::BlockActivity"){
			 		var blockXML:XML=new XML(activityXML.ns::BlockActivity);
			 		var bl:BlockActivity=new BlockActivity();
			 		bl.activitySetId=blockXML.attribute("ActivitySetId");
			 		activity.setBlockActivity(bl);
			 		trace("Block Info----------------");
			 		trace(activity.getBlockActivity().activitySetId);
			 		trace("Block Info----------------");
			 		
			 		
			 	}
			 	
			 	if(activityXML.children()[0].name().toString()==ns+"::Route"){
			 		var routeXML:XML=new XML(activityXML.ns::Route);
			 		var route:Route=new Route();
			 		route.gateWayType=routeXML.attribute("GatewayType");
			 		activity.setRoute(route);			 		
			 		trace("GateWay Info----------------");
			 		trace(activity.getRoute().gateWayType);
			 		trace("GateWay Info----------------");
			 		
			 		
			 	}
			 	
			 	var nodeGraphicXML:XML=new XML(activityXML.ns::NodeGraphicsInfos.ns::NodeGraphicsInfo);
			 	
			 	activity.getNodeGraphicsInfos().getNodeGraphicInfo().width=nodeGraphicXML.attribute("Width").toString();
			 	activity.getNodeGraphicsInfos().getNodeGraphicInfo().height=nodeGraphicXML.attribute("Height").toString();
			 	
			 	var coorXML:XML=new XML(nodeGraphicXML.ns::Coordinates);
			 	
			 	activity.getNodeGraphicsInfos().getNodeGraphicInfo().getCoordinates().x=coorXML.attribute("XCoordinate").toString();
			 	activity.getNodeGraphicsInfos().getNodeGraphicInfo().getCoordinates().y=coorXML.attribute("YCoordinate").toString();
			 	
				trace("Node Graphics Info----------------");
			    trace(activity.getNodeGraphicsInfos().getNodeGraphicInfo().width);
			    trace(activity.getNodeGraphicsInfos().getNodeGraphicInfo().height);
			    trace(activity.getNodeGraphicsInfos().getNodeGraphicInfo().getCoordinates().x);
			    trace(activity.getNodeGraphicsInfos().getNodeGraphicInfo().getCoordinates().y);
			    trace("Node Graphics Info----------------");
			 	
			 	processes.getProcess().getActivities().getActivities().addItem(activity);
			 	
			 }
			 
			 /**
			 * Transitions
			 * 
			 */ 
			 var transitionsXML:XML=new XML(xml.ns::WorkflowProcesses.ns::WorkflowProcess.ns::Transitions);
			 
			 for(i=0;i<transitionsXML.children().length();i++){
			 	var transitionXML:XML=new XML(transitionsXML.ns::Transition[i]);
			 	var transition:Transition=new Transition("","");
			 	transition.from=transitionXML.attribute("From").toString();
			 	transition.tto=transitionXML.attribute("To").toString();
			 	transition.id=transitionXML.attribute("Id").toString();
			 	transition.name=transitionXML.attribute("Name").toString();
			 	processes.getProcess().getTransitions().getTransitions().addItem(transition);
			 }
			 /**
			 * Association... 由于在子流程中, 暂时不考虑
			 * 
			 */ 
			 
			
		
		}
		


	}
}